﻿# **********************************************************************
#
# PS-Script: Export-dfs-homess
# 
# Author: Ruben
# Date:   2015-01-13
# Version: 1
#
# Description:
# This script creates a file which contains all home drives which are published 
# via DFS. It uses dfsutil to gather this information.
#
# Attention: 
# This PS script comes with ABSOLUTELY NO WARRANTY; for details
# see gnu-gpl. This is free software, and you are welcome
# to redistribute it under certain conditions; see gnu-gpl for details.
# 
# **********************************************************************

$allDFSroots = dfsutil domain $env:USERDNSDOMAIN
$dfshomesprelst = New-Object System.Collections.ArrayList
$allDFSroots | ForEach-Object { 
        $dtmp = $_ -replace('\t','')   
        if ($dtmp.Length -eq 5) {        
            $string = "\\" + $env:USERDNSDOMAIN + "\" + $dtmp   
            $dfshomesprelst.add($string)           
        }
    }


$dfshomeslst = New-Object System.Collections.ArrayList
$dfshomesprelst | ForEach-Object {
    $curdfs = $_    
    $tmpall = dfsutil root $_    
    $tmpalldfs = $tmpall  | Select-String '("Home[a-z0-9]*?")'        
    $tmpalldfs.Matches.value | ForEach-Object {
        $dfst = $_ -replace '"',''
        $dfsp = $curdfs + '\' + $dfst        
        $dfshomeslst.add($dfsp)
        }        
    }

$dfshomeinfo = New-Object System.Collections.ArrayList
$dfshomeinfo.Add("_Please choose:,0000")
$dfshomeslst | ForEach-Object {
        $out = dfsutil link $_        
        $rslt = $out -match '(\s*Target=.*\s)(\s*State="ONLINE")'            
        $rslt2 = $rslt -join " "
        if ($rslt2 -match "(\\\\\w+\\\w+)(\$)?") {
            $dfshomeinfo.Add($_ + ',' + $Matches[0])
            $rslt = ""
            $rstl2 = ""
        }                                     
    } 

$dfshomeinfo | Out-File -Encoding unicode C:\ScheduledTasks\sact\DFShomes.txt


